(function () {
    var PreLoadPro = (function () {
        function PreLoadPro() {
            PreLoadPro.super(this);

            this.proBG = null;
            this.proBar = null;

            this.init();
        }
        Laya.class(PreLoadPro, "", Laya.Box);
        var _proto = PreLoadPro.prototype;
        _proto.init = function () {
            this.size(830, 10);
            this.centerX = 0;
            this.bottom = 50;
            
            var proBG = new Laya.Image("res/start/preload_pro_bg.png");
                proBG.sizeGrid = "5,6,5,6";
                proBG.size(this.width, this.height);

            var proBar = new Laya.Image("res/start/preload_pro_bar.png");
                proBar.sizeGrid = "5,6,5,6";
                proBar.size(0, this.height);

            this.proBG = proBG;
            this.proBar = proBar;
            this.addChildren(proBG, proBar);
        }

        _proto.load = function (res, callback) {
            Laya.loader.load(res, Laya.Handler.create(this, function () {
                callback();
            }, [callback]), new Laya.Handler(this, function (value  ) {
                this.proBar.width = this.width * value;
            }));
        }
        return PreLoadPro;
    })();

    var MainLoadPro = (function () {
        function MainLoadPro() {
            MainLoadPro.super(this);

            this.proBG = null;
            this.proBar = null;
            this.rate = null;
            this.star = null;

            this.init();
        }
        Laya.class(MainLoadPro, "", Laya.Box);
        var _proto = MainLoadPro.prototype;
        _proto.init = function () {
            this.size(824, 30);
            this.visible = false;

            var proBG = new Laya.Image("res/start/mainload_pro_bg.png");
                proBG.sizeGrid = "18,18,18,18";
                proBG.size(this.width + 6, this.height + 6);
                proBG.pos(-3, -3);

            var proBar = new Laya.Image("res/start/mainload_pro_bar.png");
                proBar.sizeGrid = "15,15,15,15";
                proBar.size(0, this.height);

            var rate = new Laya.Label("0%");
                rate.align = "center";
                rate.fontSize = 22;
                rate.color = "#ffffff";
                rate.font = "Arial";
                rate.size(90, 22);
                rate.centerX = 0;
                rate.centerY = 0;

            var star = utils.createSkeleton("res/start/mainload_pro_star");
                star.scale(1.5, 1.5);
                star.pos(0, 17);
                star.play("light", true);

            this.proBG = proBG;
            this.proBar = proBar;
            this.rate = rate;
            this.star = star;
            this.addChildren(proBG, proBar, star, rate);
        }
        _proto.onProgress = function (value) {
            this.proBar.width = this.width * value;
            this.star.x = this.proBar.width;
            this.rate.text = (value * 100).toFixed(0) + "%";
        }

        _proto.load = function (res, callback) {
            Laya.loader.load(res, Laya.Handler.create(this, function () {
                callback();
            }, [callback]), new Laya.Handler(this, this.onProgress));
        }
        return MainLoadPro;
    })();
    
    (function (_super) {
        function StartScene() {
            StartScene.super(this);

            this.proPreLoad = null;
            this.proMainLoad = null;
            this.btnStartAni = null;
            this.btnStartGame = null;
            this.BGMain = null;
            this.BGStatic = null;
            this.notice = null;
            this.isbn = null;

            this.size(GAME_CONFIG.WIDTH, Laya.stage.height);
            console.log("StartScene Created. stage height:" + Laya.stage.height + "scene.width:" + this.width + "scene.height:" + this.height);
        }
        Laya.class(StartScene, "Scene.Start", _super);
        var _proto = StartScene.prototype;
        _proto.destroy = function () {
            _super.prototype.destroy.call(this, true);

            this.proPreLoad = null;
            this.proMainLoad = null;
            this.btnStartAni = null;
            this.btnStartGame = null;
            this.BGMain = null;
            this.BGStatic = null;
            this.notice = null;
            this.isbn = null;
        }

        _proto.init = function () {
            this.showStaticProgress();
            if(laya.components && laya.components.Isbn){
                this.isbn = new laya.components.Isbn();
                // this.isbn.centerX = 0;
                this.isbn.zOrder = 99;
                this.addChild(this.isbn);
            }
            this.proPreLoad.load(ASSETS.START, this.showMainView.bind(this));
        }
        _proto.showStaticProgress = function () {
            console.log("StartScene width:" + this.width + " height:" + this.height)
            var BGStatic = new Laya.Image("res/start/load_static.jpg");
                BGStatic.centerX = 0;
                BGStatic.centerY = 0;

            var proPreLoad = new PreLoadPro();

            this.proPreLoad = proPreLoad;
            this.BGStatic = BGStatic;
            this.addChildren(BGStatic, proPreLoad);
        }
        _proto.showMainView = function () {
            var BGMain = utils.createSkeleton("res/public/main")
                BGMain.pos(GAME_CONFIG.WIDTH / 2, this.height / 2);
                BGMain.play("load", true);

            var btnStartAni = utils.createSkeleton("res/start/btn_start");
                btnStartAni.pos(145, 48);
                btnStartAni.play("start", true);

            var btnStartGame = new Laya.Box();
                btnStartGame.size(290, 96)
                btnStartGame.anchorX = 0.5;
                btnStartGame.centerX = 0;
                btnStartGame.bottom = 140;
                btnStartGame.addChild(btnStartAni);
                btnStartGame.on(Laya.Event.CLICK, this, this.onStartGame);
            
            var notice = new Laya.Image("res/start/notice.png");
                notice.alpha = 0.4;
                notice.centerX = 0;
                notice.bottom = 20;
            
            var proMainLoad = new MainLoadPro();
                proMainLoad.centerX = 0;
                proMainLoad.bottom = 83;

            this.BGMain = BGMain;
            this.btnStartAni = btnStartAni;
            this.btnStartGame = btnStartGame;
            this.notice = notice;
            this.proMainLoad = proMainLoad;
            this.addChildren(BGMain, btnStartGame, notice, proMainLoad);
        }
        _proto.onStartGame = function () {
            /**
             * 先判断是否登录
             */

            if(!GM.userLogged || token == ""){
                location.href = GM.userLoginUrl;
                return;
            }

            this.btnStartAni.stop();
            this.btnStartGame.visible = false;
            this.proMainLoad.visible = true;

            this.proMainLoad.load(ASSETS.PUBLIC, function () {
                console.log("公共资源加载完毕，进入大厅界面。");
                PUBLIC_ASSETS_LOADED = true;
                
                ppt.header = new Libs.Header();
                var soundstatus = GM.muteAudio.getMuteState();

                Laya.SoundManager.musicMuted = soundstatus;
                Laya.SoundManager.soundMuted = soundstatus;

                Laya.SoundManager.playMusic("sound/bgm.mp3");
                ppt.director.runScene(new Scene.Home);
            });
        }

        _proto.onEnter = function () {
            Laya.loader.load(ASSETS.PRESTART, Laya.Handler.create(this, this.init));
        }
        _proto.onExit = function () {
            this.destroy(true);

            this.proPreLoad = null;
            this.proMainLoad = null;
            this.btnStartAni = null;
            this.btnStartGame = null;
            this.BGMain = null;
            this.BGStatic = null;
            this.notice = null;
        }
        _proto.onResize = function (height) {
            this.height = height;

            this.notice && (this.notice.bottom = 50);
            this.btnStartGame && (this.btnStartGame.bottom = 140);
            this.BGMain && (this.BGMain.y = height / 2);
            this.proMainLoad && (this.proMainLoad.bottom = 83);
        }
    })(Laya.Sprite);
})();